package com.zhidisoft.admin.controller;

import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhidisoft.admin.entity.Article;
import com.zhidisoft.admin.entity.ArticleType;
import com.zhidisoft.admin.service.IArticleService;
import com.zhidisoft.admin.service.IArticleTypeService;
import com.zhidisoft.admin.vo.ArticleVo;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

	@Autowired
	private IArticleService articleService;
	@Autowired
	private IArticleTypeService articleTypeService;
	
	@GetMapping("/save")
	public ModelAndView saveUi(){
		List<ArticleType> articleTypes = articleTypeService.geArticleTypes();
		ModelAndView mav = new ModelAndView("article/article-save.html");
		mav.addObject("typeList",articleTypes);
		return mav;
	}
	
	/**
	 * @Valid 400报错可能问题 BindingResult要在HttpSession前  参数顺序不对 <br/>
	 * @Valid Article article, BindingResult result, HttpSession session <br/>
	 * @param article
	 * @param result
	 * @param session
	 * @param redirectAttributes
	 * @return
	 */
	@PostMapping("/save")
	public ModelAndView save(@Valid ArticleVo articleVo, BindingResult result,RedirectAttributes redirectAttributes){
		ModelAndView mav = new ModelAndView("redirect:/article/articleList");
		if (result.hasErrors()) {
			/*有两种方法：
			 * 1，转发
			 * 	mav = new ModelAndView("/common/error.html");
			 *	mav.addObject("errorList", result.getAllErrors());
			 *  return "redirect:/error";
			 * 2，重定向传递参数
			 * */
			mav = new ModelAndView("redirect:/common/err");
			redirectAttributes.addFlashAttribute("errorList", result.getAllErrors());
			return mav;
		}
		Article article = new Article();
		article.setId(UUID.randomUUID().toString().replace("-", ""));
		article.setUserId(getCurUser());
		article.setInfo(articleVo.getInfo());
		article.setInfoTitle(articleVo.getInfoTitle());
		article.setTypeId(articleVo.getTypeId());
		article.setTitle(articleVo.getTitle());
		articleService.save(article);
		return mav;
	}
	
	/**
	 * TODO 点击删除时，没有跳转到controller
	 * @param id
	 */
	@PostMapping("/delete")
	public void Delete(String id){
		articleService.deleteById(id);
	}
	
	/**
	 * ModelAndView方法
	 * 文章列表
	 * @return
	 */
	@GetMapping("/articleList")	
	public ModelAndView articleModelAndView(@RequestParam(name="pageNum",defaultValue="1") Integer pageNum,@RequestParam(name="pageSize",defaultValue="5")Integer pageSize){
		ModelAndView mav = new ModelAndView("article/article-table.html","list",articleService.findPage(pageNum, pageSize));	
		mav.addObject("typeList", articleTypeService.geArticleTypes());
		return mav;	
	}
	
}
